package demo.common.model;

import com.google.common.base.CaseFormat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 排除值为null的属性
 * Created by llq on 2017/12/20.
 */
public class SqlCondition extends HashMap {

    private List<Eq> conditions = new ArrayList<Eq>();

    public SqlCondition eq(String propName, Object value) {
        this.put(propName, value);

        Eq eq = new Eq();
        eq.propName = propName;
        eq.value = value;
        conditions.add(eq);
        return this;
    }

    public static SqlCondition create() {
        return new SqlCondition();
    }

    public List<String> whereSQL() {
        List<String> sqls = new ArrayList<>();
        for (Eq condition : conditions) {
            sqls.add(propertyName2TableFieldName(condition.propName) + " =#{" + condition.propName + "}");
        }
        return sqls;
    }

    public static String propertyName2TableFieldName(String propertyName) {
        return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, propertyName);
    }

    public static class Eq {
        public String propName;
        public Object value;
    }

}
